package com.power.using.myapplication.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.power.using.myapplication.R;
import com.power.using.myapplication.okhttp.CustomCallBack;
import com.power.using.myapplication.okhttp.HttpNetWorkUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import okhttp3.Request;

public class MainActivity extends AppCompatActivity {

    private TextView tv1;
    private ProgressBar pb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1 = (TextView) findViewById(R.id.tv1);
        pb = (ProgressBar) findViewById(R.id.pb);
    }

    public void Click1(View view) {
        final String url = "http://www.baidu.com";
        new Thread() {
            @Override
            public void run() {
                final String resultStr = HttpNetWorkUtil.getInstance().getStringSync(url);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText(resultStr);
                    }
                });
            }
        }.start();


    }

    public void Click2(View view) {
        final String url = "http://www.baidu.com";
        new Thread() {
            @Override
            public void run() {
                byte[] bytes = HttpNetWorkUtil.getInstance().getByteArraySync(url);
                final StringBuilder builder = new StringBuilder();
                for (byte b : bytes) {
                    builder.append(b);
                }

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText(builder.toString());
                    }
                });
            }
        }.start();

    }

    public void Click3(View view) {
        final String url = "http://www.baidu.com";
        new Thread() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();
                try {
                    InputStream in = HttpNetWorkUtil.getInstance().getInputStreamSync(url);
                    int len = 0;
                    byte[] bytes = new byte[1024];


                    while ((len = in.read(bytes)) != -1) {
                        builder.append(len);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText(builder.toString());
                    }
                });
            }
        }.start();

    }

    public void Click4(View view) {
        final String url = "http://www.baidu.com";
        new Thread() {
            @Override
            public void run() {
                final StringBuilder builder = new StringBuilder();
                try {
                    Reader in = HttpNetWorkUtil.getInstance().getCharReaderSync(url);
                    int len = 0;
                    while ((len = in.read()) != -1) {
                        builder.append(len);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }


                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tv1.setText(builder.toString());
                    }
                });
            }
        }.start();

    }


    public void Click5(View view) {

        //https://www.baidu.com/s?ie=UTF-8&wd=%E5%93%88%E5%A3%AB%E5%A5%87
        String url="https://www.baidu.com/s?ie=UTF-8&wd=哈士奇";
        HttpNetWorkUtil.getInstance().getStringWithoutParamsAsync(url, new CustomCallBack<String>() {
            @Override
            public void onBefore(Request request) {
                pb.setVisibility(View.VISIBLE);
            }

            @Override
            public void requestSuccess(String result) {

                tv1.setText(result);
            }

            @Override
            public void requestFailure(Request request, IOException exception) {
                tv1.setText(exception.getMessage());
            }

            @Override
            public void onAfter() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pb.setVisibility(View.GONE);
                    }
                });

            }
        });

    }
    public void Click6(View view) {

    }
    public void Click7(View view) {

    }
    public void Click8(View view) {

    }
}
